/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.admin.company;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.entity.company.CompanyType;
import com.cos.service.admin.company.CompanyTypeService;
import com.cos.util.AppUtil;
import com.cos.util.Const;
import com.cos.util.PageData;
import com.cos.util.Jurisdiction;

/**
 * 公司类型控制层
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">jinxf</a>
 * @version  Date: 2016年3月14日 上午9:59:42
 * @serial 1.0
 * @since 2016年3月14日 上午9:59:42
 */
@Controller
@RequestMapping(value="/companyType")
public class CompanyTypeController extends BaseController {
	
	String menuUrl = "companyType/list.do"; //菜单地址(权限用)
	@Resource(name="companyTypeService")
	private CompanyTypeService companyTypeService;
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public Object save(CompanyType companyType) throws Exception{
		logBefore(logger, "CompanyType");
		PageData pd = new PageData();
		Map<String,String> map = new HashMap<String,String>();
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){
			pd.put("ID", this.get32UUID());			//主键
			pd.put("TYPENAME", companyType.getTYPENAME());	//文件名
			pd.put("PARENT_ID",companyType.getPARENT_ID() );//路径
		
			companyTypeService.save(pd);
		
		} //
		map.put("result", "ok");
		return AppUtil.returnObject(pd, map);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger, "删除企业类别");
		PageData pd = new PageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){
				pd = this.getPageData();
				companyTypeService.delete(pd);
			}
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit(HttpServletRequest request,	
			@RequestParam(value="ID",required=false) String ID,
			@RequestParam(value="TYPENAME",required=false) String TYPENAME,
			@RequestParam(value="PARENT_ID",required=false) int PARENT_ID) throws Exception{
		logBefore(logger, "修改类型");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){
			pd.put("ID", ID);		//ID 
			pd.put("TYPENAME", TYPENAME);					//类型名字
			pd.put("PARENT_ID", PARENT_ID);			//属于ID
			
			companyTypeService.edit(pd);				//执行修改数据库
		}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表Company");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String KEYW = pd.getString("keyword");
			
			if(null != KEYW && !"".equals(KEYW)){
				KEYW = KEYW.trim();
				pd.put("KEYW", KEYW);
			}
			page.setPd(pd);
			List<PageData>	varList = companyTypeService.list(page);	//列出列表
			mv.setViewName("admin/company/companyType_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 新增页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd() throws UnsupportedEncodingException{
		logBefore(logger, "去新增界面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("admin/company/companyType_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 修改页面
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit() throws UnsupportedEncodingException{
		logBefore(logger, "去修改界面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = companyTypeService.findById(pd);	//鏍规嵁ID璇诲彇
			mv.setViewName("admin/company/companyType_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除companyType");
		PageData pd = new PageData();		
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){
				List<PageData> pdList = new ArrayList<PageData>();
				String DATA_IDS = pd.getString("DATA_IDS");
				if(null != DATA_IDS && !"".equals(DATA_IDS)){
					String ArrayDATA_IDS[] = DATA_IDS.split(",");
					companyTypeService.deleteAll(ArrayDATA_IDS);
					pd.put("msg", "ok");
				}else{
					pd.put("msg", "no");
				}
				pdList.add(pd);
				map.put("list", pdList);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
}
	
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
